'use strict';
const { RUN_ACT, ROLL_ACT, JUMP_ACT, JUMP_HEIGHT,
    BACK_MOVE_TIME_1, BACK_MOVE_TIME_2, BACK_MOVE_TIME_3, BACK_MOVE_TIME_4, BACK_MOVE_TIME_5, FLOOR_MOVE_TIME
} = require('const');

cc.Class({
    extends: cc.Component,

    properties: {
        mHero: cc.Animation,
        // roll button
        btnRoll: cc.Node,
        // // jump
        // btnJump: cc.Node,
        // slow background
        mBack1: [cc.Node],
        // faster background
        mBack2: [cc.Node],
        // fastest background
        mBack3: [cc.Node],
        // wall
        mBack4: [cc.Node],
        // nearest sea
        mBack5: [cc.Node],
        // floor
        mFloor: [cc.Node],
    },

    // LIFE-CYCLE CALLBACKS:

    // default animation is run
    onLoad () {
        this.play(RUN_ACT);
        cc.log('on load');
        // register click event
        // roll
        this.btnRoll.on(cc.Node.EventType.TOUCH_START, this.onBtnRollStart, this);
        this.btnRoll.on(cc.Node.EventType.TOUCH_END, this.onBtnRollEnd, this);
        this.btnRoll.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnRollEnd, this);

        // move background
        const bg = [this.mBack1, this.mBack2, this.mBack3, this.mBack4, this.mBack5, this.mFloor];
        const moveTimes = [ BACK_MOVE_TIME_1, BACK_MOVE_TIME_2, BACK_MOVE_TIME_3, BACK_MOVE_TIME_4,
            BACK_MOVE_TIME_5, FLOOR_MOVE_TIME ];
        for(let i = 0; i < bg.length; ++i) {
            const mt = moveTimes[i];
            for (let j = 0; j < bg[i].length; ++j) {
                const width = bg[i][j].width;
                // 设置位置
                bg[i][j].setPosition(j*width, 0);
                const moveStep = cc.moveTo((j+1)*mt, cc.v2(-(width), 0));
                const seq = cc.sequence(moveStep, cc.callFunc(this.backMoveEnd, this, mt));
                bg[i][j].runAction(seq);
            }
        }
    },
    // 循环移动背景
    backMoveEnd: function(target, data) {
        const width = target.width;
        target.setPosition(width, 0);
        const move = cc.moveTo(data*2, cc.v2(-(width), 0));
        const seq = cc.sequence(move, cc.callFunc(this.backMoveEnd, this, data));
        target.runAction(seq);
    },

    // start () {
    // },
    onBtnRollStart: function () {
        cc.log('touch roll start');
        if(this.mHero.currentClip.name === JUMP_ACT) {
            return
        }
        this.play(ROLL_ACT);
    },

    onBtnRollEnd: function () {
        cc.log('touch roll end');
        if(this.mHero.currentClip.name === JUMP_ACT) {
            return
        }
        this.play(RUN_ACT);
        cc.log('play run act');
    },

    backToRun: function() {
        this.play(RUN_ACT);
    },

    play(data) {
        if (!this.isCanChangeClip(data)) {
            // can not play animation
            return;
        }
        switch (data) {
            case ROLL_ACT:
            case RUN_ACT:
                this.mHero.play(data);
                break;
            case JUMP_ACT:
                // copy from simple action in cocos example
                const moveUpAction = cc.moveBy(1, cc.v2(0, JUMP_HEIGHT)).easing(cc.easeCubicActionOut());
                const moveDownAction = cc.moveBy(1, cc.v2(0, -JUMP_HEIGHT)).easing(cc.easeCubicActionIn());
                const seq = cc.sequence(moveUpAction, moveDownAction, cc.callFunc(this.backToRun.bind(this)));
                this.mHero.node.runAction(seq);
                this.mHero.play(JUMP_ACT);
                break;
        }
    },

    isCanChangeClip: function(playName) {
        //判断滑铲
        if ( playName === ROLL_ACT) {
            //如果是跳跃动画,返回否
            if( this.mHero.currentClip.name === JUMP_ACT ) {
                return false;
            } else if ( this.mHero.currentClip.name === RUN_ACT) {
                //如果是跑动,返回可以
                return true;
            }
        } else if ( playName === JUMP_ACT) {
            //如果是跑动,可以
            return this.mHero.currentClip.name === RUN_ACT;
        }
        //其他动作都可以
        return true;
    },

    // trigger roll animation
    onBtnChange(target, data) {
        this.play(data);
    },
    // update (dt) {},
});
